#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>

const char *filename = "log.txt";

int main()
{
    // 4 缓冲区
   printf("hello printf\n");
   fprintf(stdout, "hello fprintf\n");
   const char *msg = "hello write\n";
   write(1, msg, strlen(msg));

   fork();

    //// 3 dup2
    ////int fd = open("dev/pts/2", O_CREAT | O_WRONLY | O_TRUNC, 0666);
    //int fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666);
    //if(fd < 0)
    //{
    //    perror("open fail\n");
    //    return 1;
    //}

    //dup2(fd, 1);

    //printf("hello printf\n");
    //fprintf(stdout, "hello fprintf\n");

    //fflush(stdout);

    //// 2
    //int fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666);
    //if(fd < 0)
    //{
    //    perror("open fail\n");
    //    return 1;
    //}
    //printf("printf, fd: %d\n", fd);
    //fprintf(stdout, "fprintf, fd: %d\n", fd);

    //fflush(stdout);

    //close(fd);

    //// 1 read
    ////int fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666);
    //struct stat st;
    //int n = stat(filename, &st);
    //if(n < 0)   return 1;

    //printf("file size: %lu\n", st.st_size);

    ////int fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666);
    //int fd = open(filename, O_RDONLY);
    //if(fd < 0)
    //{
    //    perror("open fail\n");
    //    return 2;
    //}
    //printf("fd: %d\n", fd);
   
    //char *file_buffer = (char*)malloc(st.st_size+1);

    //n = read(fd, file_buffer, st.st_size);
    //if(n > 0)
    //{
    //    file_buffer[n] = '\0';
    //    printf("\n%s", file_buffer);
    //}
    //free(file_buffer);
    //
    ////const char *message = "hello Linux file!\n";
    ////write(fd, message, strlen(message));
    ////write(fd, message, strlen(message));
    ////write(fd, message, strlen(message));

    //close(fd);

    return 0;
}
